package br.edu.universo.si.projeto2.infojob.client.componentes.comboboxes;

import java.util.HashMap;

import br.edu.universo.si.projeto2.infojob.client.util.Util;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;

public class SetorAtuacaoListBox extends ListBox 
{
	private Integer indice = 0;
	private Boolean callback = false;
	public SetorAtuacaoListBox()
	{
		super(false);
		this.addItem("", "");
		this.setWidth("300px");

		AsyncCallback<HashMap<Integer, String>> callback = new AsyncCallback<HashMap<Integer, String>>() {
	
			public void onSuccess(HashMap<Integer, String> result)
			{
				for (int x = 1; x <= result.size(); x++)
				{
					SetorAtuacaoListBox.this.addItem(result.get(x), Integer.toString(x));
				}
				//Setando o indice no listbox
				SetorAtuacaoListBox.this.setSelectedIndex(SetorAtuacaoListBox.this.getIndice());
				SetorAtuacaoListBox.this.setCallback(true);
				SetorAtuacaoListBox.this.atauliza();
			}

			public void onFailure(Throwable caught) { }
		};

		Util.getService().getSetoresAtuacao(callback); 
	}
	public void setIndice(Integer indice) {
		this.indice = indice;
	}
	public Integer getIndice() {
		return indice;
	}
	public void setCallback(Boolean callback) {
		this.callback = callback;
	}
	public Boolean getCallback() {
		return callback;
	} 
	public void atauliza(){
		if(callback){
			SetorAtuacaoListBox.this.setSelectedIndex(SetorAtuacaoListBox.this.getIndice());
		}
	}
}
